package Login;


/**
 * @BelongsProject: iobook
 * @BelongsPackage: Login
 * @Author: ByHappy  (Email:15188881427@163.c0m)
 * @CreateTime: 2023-08-05 16:37
 * @Description: TODO
 * @Version: 1.0
 **/


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class UserDaoImpl implements UserDao {
    private static File file = new File("test_user.txt");

    static {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void regist(User user) {
        String info = user.getUserName() + "=" + user.getPassword();
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
            bw.write(info);
            bw.newLine();
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean isLogin(String name, String password) {
        boolean flag = false;
        String line;
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));

            try {
                while ((line = br.readLine()) != null) {
                    String[] strings = line.split("=");
                    if (name.equals(strings[0]) && password.equals(strings[1])) {
                        flag = true;
                    }
                }
            } catch (IOException e) {

                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            //
            e.printStackTrace();
        }
        return flag;
    }
}


